/*
 * Copyright (C) 2014 Eduardo Pereira Montenegro <epereiramontenegro@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.bambole.domain;

import com.googlecode.bambole.controller.util.JsfUtil;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Eduardo Pereira Montenegro <epereiramontenegro@gmail.com>
 */
@Entity
@Table(name = "order_header")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "OrderHeader.findAll", query = "SELECT o FROM OrderHeader o"),
    @NamedQuery(name = "OrderHeader.findById", query = "SELECT o FROM OrderHeader o WHERE o.id = :id"),
    @NamedQuery(name = "OrderHeader.findMaxDocument", query = "SELECT MAX(o.document) FROM OrderHeader o"),
    @NamedQuery(name = "OrderHeader.findByDocument", query = "SELECT o FROM OrderHeader o WHERE o.document = :document")})
public class OrderHeader implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Column(name = "created")
    @Temporal(TemporalType.TIMESTAMP)
    private Date created;
    @Basic(optional = false)
    @NotNull
    @Column(name = "updated")
    @Temporal(TemporalType.TIMESTAMP)
    private Date updated;
    @Column(name = "freight_estimated_cost")
    private double freightEstimatedCost;
    @Column(name = "freight_estimated_transit_time")
    private Integer freightEstimatedTransitTime;
    @Basic(optional = false)
    @NotNull
    @Column(name = "ordered")
    @Temporal(TemporalType.DATE)
    private Date ordered;
    @Basic(optional = false)
    @NotNull
    @Column(name = "estimated_delivery_date")
    @Temporal(TemporalType.DATE)
    private Date estimatedDeliveryDate;
    @Basic(optional = false)
    @NotNull
    @Column(name = "document")
    private int document;
    @Size(max = 32)
    @Column(name = "reference")
    private String reference;
    @Size(max = 128)
    @Column(name = "description")
    private String description;
    @Basic(optional = false)
    @NotNull
    @Column(name = "product_amount")
    private double productAmount;
    @Basic(optional = false)
    @NotNull
    @Column(name = "product_discount")
    private double productDiscount;
    @Basic(optional = false)
    @NotNull
    @Column(name = "discounted_product_amount")
    private double discountedProductAmount;
    @Basic(optional = false)
    @NotNull
    @Column(name = "total_discount")
    private double totalDiscount;
    @Basic(optional = false)
    @NotNull
    @Column(name = "discount")
    private double discount;
    @Basic(optional = false)
    @NotNull
    @Column(name = "amount")
    private double amount;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "orderHeader")
    private List<ShipmentHeader> shipmentHeaderList;
    @JoinColumn(name = "updated_by", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private User updatedBy;
    @JoinColumn(name = "status", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Status status;
    @JoinColumn(name = "freight_organization_person", referencedColumnName = "id")
    @ManyToOne
    private Person freightOrganizationPerson;
    @JoinColumn(name = "person", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Person person;
    @JoinColumn(name = "payment_term", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private PaymentTerm paymentTerm;
    @JoinColumn(name = "type", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Type type;
    @JoinColumn(name = "channel", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Channel channel;
    @JoinColumn(name = "freight_organization", referencedColumnName = "id")
    @ManyToOne
    private Organization freightOrganization;
    @JoinColumn(name = "organization", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Organization organization;
    @JoinColumn(name = "created_by", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private User createdBy;
    @JoinColumn(name = "address", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Address address;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "orderHeader")
    private List<OrderItem> orderItemList;

    public OrderHeader() {
    }

    public OrderHeader(Integer id) {
        this.id = id;
    }

    public OrderHeader(Integer id, Date created, Date updated, Date ordered, int document, double productAmount, double productDiscount, double discount, double amount) {
        this.id = id;
        this.created = created;
        this.updated = updated;
        this.ordered = ordered;
        this.document = document;
        this.productAmount = productAmount;
        this.productDiscount = productDiscount;
        this.discount = discount;
        this.amount = amount;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getUpdated() {
        return updated;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    public double getFreightEstimatedCost() {
        return freightEstimatedCost;
    }

    public void setFreightEstimatedCost(double freightEstimatedCost) {
        this.freightEstimatedCost = freightEstimatedCost;
    }

    public Integer getFreightEstimatedTransitTime() {
        return freightEstimatedTransitTime;
    }

    public void setFreightEstimatedTransitTime(Integer freightEstimatedTransitTime) {
        this.freightEstimatedTransitTime = freightEstimatedTransitTime;
    }

    public Date getOrdered() {
        return ordered;
    }

    public void setOrdered(Date ordered) {
        this.ordered = ordered;
    }

    public Date getEstimatedDeliveryDate() {
        return estimatedDeliveryDate;
    }

    public void setEstimatedDeliveryDate(Date estimatedDeliveryDate) {
        this.estimatedDeliveryDate = estimatedDeliveryDate;
    }

    public String getReference() {
        return reference;
    }

    public void setReference(String reference) {
        this.reference = reference;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getDocument() {
        return document;
    }

    public void setDocument(int document) {
        this.document = document;
    }

    public double getProductAmount() {
        return productAmount;
    }

    public void setProductAmount(double productAmount) {
        this.productAmount = productAmount;
    }

    public double getProductDiscount() {
        return productDiscount;
    }

    public void setProductDiscount(double productDiscount) {
        this.productDiscount = productDiscount;
    }

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public double getDiscountedProductAmount() {
        return discountedProductAmount;
    }

    public void setDiscountedProductAmount(double discountedProductAmount) {
        this.discountedProductAmount = discountedProductAmount;
    }

    public double getTotalDiscount() {
        return totalDiscount;
    }

    public void setTotalDiscount(double totalDiscount) {
        this.totalDiscount = totalDiscount;
    }

    public String getFormattedDocument() {
        return String.format(JsfUtil.getLocale(), "%,d", getDocument());
    }

    @XmlTransient
    public List<ShipmentHeader> getShipmentHeaderList() {
        return shipmentHeaderList;
    }

    public void setShipmentHeaderList(List<ShipmentHeader> shipmentHeaderList) {
        this.shipmentHeaderList = shipmentHeaderList;
    }

    public User getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(User updatedBy) {
        this.updatedBy = updatedBy;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public Person getFreightOrganizationPerson() {
        return freightOrganizationPerson;
    }

    public void setFreightOrganizationPerson(Person freightOrganizationPerson) {
        this.freightOrganizationPerson = freightOrganizationPerson;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public PaymentTerm getPaymentTerm() {
        return paymentTerm;
    }

    public void setPaymentTerm(PaymentTerm paymentTerm) {
        this.paymentTerm = paymentTerm;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public Organization getFreightOrganization() {
        return freightOrganization;
    }

    public void setFreightOrganization(Organization freightOrganization) {
        this.freightOrganization = freightOrganization;
    }

    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    public User getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(User createdBy) {
        this.createdBy = createdBy;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String getFormattedAmount() {
        return String.format(JsfUtil.getLocale(), "%,.2f", getAmount());
    }

    @XmlTransient
    public List<OrderItem> getOrderItemList() {
        return orderItemList;
    }

    public void setOrderItemList(List<OrderItem> orderItemList) {
        this.orderItemList = orderItemList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof OrderHeader)) {
            return false;
        }
        OrderHeader other = (OrderHeader) object;
        return (this.id != null || other.id == null) && (this.id == null || this.id.equals(other.id));
    }

    @Override
    public String toString() {
        return "OrderHeader[" + id + "]";
    }

}
